package com.rupeng.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.junit.runner.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rupeng.pojo.AdminUser;
import com.rupeng.pojo.Role;
import com.rupeng.service.AdminUserService;
import com.rupeng.service.RoleService;
import com.rupeng.util.AjaxResult;
import com.rupeng.util.CommonUtils;
import com.rupeng.util.ImageCodeUtils;

@Controller
@RequestMapping("/adminUser")
public class AdminUserController {
	@Autowired
	private AdminUserService adminUserService;
	@Autowired
	private RoleService roleService;
	@RequestMapping(value="/login.do",method=RequestMethod.GET)
	public ModelAndView loginPage() {
		return new ModelAndView("adminUser/login");
	}
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	public ModelAndView loginSubmit(String account, String password, String imageCode, HttpServletRequest req) {
		if (!ImageCodeUtils.checkImageCode(req.getSession(), imageCode)) {
			return new ModelAndView("adminUser/login", "message", "验证码不正确");
		}
		AdminUser adminUser = adminUserService.login(account,password);
		if (adminUser==null) {
			return new ModelAndView("adminUser/login", "message", "账号或密码错误");
		} else {
			req.getSession().setAttribute("adminUser", adminUser);
			return new ModelAndView("redirect:/");
		}
	}
	@RequestMapping("/logout.do")
	public ModelAndView logout(HttpServletRequest req) {
		req.getSession().invalidate();
		return new ModelAndView("redirect:/adminUser/login.do");
	}
	@RequestMapping("/list.do")
	public ModelAndView list() {
		List<AdminUser> adminUsers = adminUserService.selectList();
		return new ModelAndView("adminUser/list", "adminUsers", adminUsers);
	}
	@RequestMapping(value="/add.do",method=RequestMethod.GET)
	public ModelAndView addPage() {
		List<Role> roles = roleService.selectList();
		return new ModelAndView("adminUser/add", "roles", roles);
	}
	@RequestMapping(value="/add.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult addSubmit(String account, String password, Long[] roleIds){
		if (CommonUtils.isEmpty(account)&&CommonUtils.isEmpty(password)) {
			return AjaxResult.errorInstance("表格有字段为空");
		}
		if (!CommonUtils.isLengthEnough(account, 2)) {
			return AjaxResult.errorInstance("用户名长度不够");
		}
		if (!CommonUtils.isLengthEnough(password, 2)) {
			return AjaxResult.errorInstance("密码长度不够");
		}
		AdminUser adminUser = new AdminUser();
		adminUser.setAccount(account);
		if (adminUserService.isExisted(adminUser)) {
			return AjaxResult.errorInstance("此用户名已存在");
		}
		adminUser.setPassword(password);
		adminUserService.insert(adminUser, roleIds);
		return AjaxResult.successInstance("管理员用户添加成功");
	}
	@RequestMapping("/disable.do")
	public @ResponseBody AjaxResult disable(Long id) {
		AdminUser adminUser = adminUserService.selectOne(id);
		adminUser.setIsDisabled(!adminUser.getIsDisabled());
		adminUserService.update(adminUser);
		if (adminUser.getIsDisabled()) {
			return AjaxResult.successInstance("已禁用该账户");
		} else {
			return AjaxResult.successInstance("已启用该账户");
		}
	}
	@RequestMapping("/delete.do")
	public @ResponseBody AjaxResult delete(Long id) {
		adminUserService.delete(id);
		return AjaxResult.successInstance("该用户已删除");
	}
	@RequestMapping(value="/resetPassword.do",method=RequestMethod.GET)
	public ModelAndView resetPasswordPage(Long id) {
		AdminUser adminUser = adminUserService.selectOne(id);
		return new ModelAndView("adminUser/resetPassword", "adminUser", adminUser);
	}
	@RequestMapping(value="/resetPassword.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult resetPasswordSubmit(Long id, String password, HttpServletRequest req) {
		if (!CommonUtils.isLengthEnough(password, 6)) {
			return AjaxResult.errorInstance("密码强度不够");
		}
		adminUserService.resetPassword(id, password);
		AdminUser adminUser = (AdminUser) req.getSession().getAttribute("adminUser");
		if (adminUser.getId()==id) {
			req.getSession().invalidate();
			return AjaxResult.successInstance("本用户密码修改成功，请重新登录");
		}
		return AjaxResult.successInstance("该用户密码修改成功");
	}
	@RequestMapping(value="/updatePassword.do",method=RequestMethod.GET)
	public ModelAndView updatePasswordPage(Long id) {
		return new ModelAndView("adminUser/updatePassword", "id", id);
	}
	@RequestMapping(value="/updatePassword.do",method=RequestMethod.POST)
	public @ResponseBody AjaxResult updatePasswordSubmit(String password, String newpassword, String renewpassword, HttpServletRequest req) {
		AdminUser adminUser = (AdminUser)req.getSession().getAttribute("adminUser");
		if (!newpassword.equals(renewpassword)) {
			return AjaxResult.errorInstance("两次密码不一致");
		}
		if (adminUserService.login(adminUser.getAccount(), password)==null) {
			return AjaxResult.errorInstance("原密码错误");
		}
		if (!CommonUtils.isLengthEnough(newpassword, 6)) {
			return AjaxResult.errorInstance("密码强度不够");
		}
		adminUserService.resetPassword(adminUser.getId(), newpassword);
		req.getSession().invalidate();
		return AjaxResult.successInstance("用户密码修改成功，请用新密码登录");
	}
}
